*******************************************************************************
* Prepare the HSW direct interview data for wave 3

use "$data_input/OWWA_HSW_Wave3.dta", clear

* First cleaning
missings dropvars, force
compress

drop if rno==.c

* Define locals with groups of variables
local desc_employer "hh_d1_description_employer_viole hh_d1_description_employer_stric hh_d1_description_employer_slave hh_d1_description_employer_short hh_d1_description_employer_good_"
local emp_status "h_a8_change_emp_3 h_a19_change_emp_3 h_a2_terminated_3 h_a17_reapply_abroad_3 h_a12_work_3"
local reason_leftemp "h_a6_reason_left_homesick_3 h_a6_reason_left_homesick_3 h_a6_reason_left_illness_3 h_a6_reason_left_hunger_3 h_a6_reason_left_accident_3 h_a6_reason_left_working_hrs_3 h_a6_reason_left_overworked_3 h_a6_reason_left_expect_3 h_a6_reason_left_salary_delays_3 h_a6_reason_left_salary_deduct_3 h_a6_reason_left_no_restday_3 h_a6_reason_left_no_week_rest_3 h_a6_reason_left_privacy_3 h_a6_reason_left_maltreat_3 h_a6_reason_left_sexual_3 h_a6_reason_left_threats_3 h_a6_reason_left_coworkers_3 h_a6_reason_left_emp_hh_3 h_a6_reason_left_employer_left_3 h_a6_reason_left_passed_away_3 h_a6_reason_left_death_family_3 h_a6_reason_left_strict_comm_3 h_a6_reason_left_ill_family_3 h_a6_reason_left_marital_3 h_a6_reason_left_others_3 h_a26_reason_left_end_contract_3"
local reason_changeemp "h_a10_reason_change_homesick_3 h_a10_reason_change_illness_3 h_a10_reason_change_hunger_3 h_a10_reason_change_accident_3 h_a10_reason_change_working_hrs_ h_a10_reason_change_overworked_3 h_a10_reason_change_expect_3 h_a10_reason_change_salary_delay h_a10_reason_change_salary_deduc h_a10_reason_change_no_restday_3 h_a10_reason_change_no_week_rest h_a10_reason_change_privacy_3 h_a10_reason_change_maltreat_3 h_a10_reason_change_sexual_3 h_a10_reason_change_threats_3 h_a10_reason_change_coworkers_3 h_a10_reason_change_emp_  h_a10_reason_change_employer_lef h_a10_reason_change_passed_away_ h_a10_reason_change_strict_comm_"
local reason_changemp "h_a22_reason_change_emp_homesick h_a22_reason_change_emp_illness_ h_a22_reason_change_emp_hunger_3 h_a22_reason_change_emp_accident h_a22_reason_change_emp_working_ h_a22_reason_change_emp_overwork h_a22_reason_change_emp_expect_3 h_a22_reason_change_emp_salary_d h_a22_reason_change_emp_salary_0 h_a22_reason_change_emp_no_restd h_a22_reason_change_emp_no_week_ h_a22_reason_change_emp_privacy_ h_a22_reason_change_emp_maltreat h_a22_reason_change_emp_sexual_3 h_a22_reason_change_emp_threats_ h_a22_reason_change_emp_coworker h_a22_reason_change_emp_emp_hh_3 h_a22_reason_change_emp_employer h_a22_reason_change_emp_passed_a h_a22_reason_change_emp_strict_c"
local treatment "h_d10_provide_food_3 h_d11_training_3 h_d14_work_break_3 h_d17_leave_restday_3 h_d18_ever_sick_3 "
local intervention "h_d21_dried_mangoes_3 h_e31_savings_reminder_3"
local fam_coord "h_e4_disagree_remit_3  h_e17_excess_remit_3  h_e18_family_ask_more_3"
local bank_account "h_e20_acct_ph_3 h_e21_acct_abroad_3 h_e28_savings_higher_3 "
local savings_purpose "h_e25_purpose_savings_food_3 h_e25_purpose_savings_food_3 h_e25_purpose_savings_clothes_3 h_e25_purpose_savings_rent_3 h_e25_purpose_savings_house_3 h_e25_purpose_savings_renovate_3 h_e25_purpose_savings_med_3 h_e25_purpose_savings_educ_3 h_e25_purpose_savings_car_3 h_e25_purpose_savings_durable_3 h_e25_purpose_savings_investment h_e25_purpose_savings_emig_3 h_e25_purpose_savings_emergency_ h_e25_purpose_savings_debts_3 h_e25_purpose_savings_retire_3 h_e25_purpose_savings_insurance_ h_e25_purpose_savings_bills_3 h_e25_purpose_savings_otherdaily h_e25_purpose_savings_children_3 h_e25_purpose_savings_none_3"
local use_savings "h_e30_used_savings_food_3 h_e30_used_savings_clothes_3 h_e30_used_savings_rent_3 h_e30_used_savings_house_3 h_e30_used_savings_renovate_3 h_e30_used_savings_med_3 h_e30_used_savings_educ_3 h_e30_used_savings_car_3 h_e30_used_savings_durable_3 h_e30_used_savings_investment_3 h_e30_used_savings_emig_3 h_e30_used_savings_emergency_3 h_e30_used_savings_debts_3 h_e30_used_savings_retire_3 h_e30_used_savings_insurance_3 h_e30_used_savings_bills_3 h_e30_used_savings_otherdaily_3 h_e30_used_savings_children_3"

* Binary variables gift treatment
gen remember_gift_3 = h_d21_dried_mangoes_3
replace remember_gift_3 = 0 if h_d21_dried_mangoes_3 == .a // don't know is counted as no

gen gave_gift_3 = 1 if h_d22_dried_mangoes_employer_3<. 
replace gave_gift_3 = 0 if h_d22_dried_mangoes_employer_3 == 4 | remember_gift_3==0 | h_d22_dried_mangoes_employer_3== .a // don't know is counted as no
lab var remember_gift_3 "Remembers gift treatment"
lab var gave_gift_3 "Gave gift to employer"

* Binary variable of savings reminders
gen remember_savrem_3 = h_e31_savings_reminder_3
lab var remember_savrem "Remembers savings reminders"

* Note: HSWs still abroad were asked the same questions as HSWs back in the Philippines 
* but with a slightly different wording. For all sections after a, TNS stored the 
* answers in the same variable. However, in section a we have to combine the variables
* ourselves.

gen     h_change_emp_3 = h_a19_change_emp_3 if h_a8_change_emp_3==.c
replace h_change_emp_3 = h_a8_change_emp_3 if h_a19_change_emp_3==.c

* Create and format all date variables
* Departure date according to the HSW (not used)
gen leave_date_3=.c
replace leave_date_3= h_a3_leave_ph_date_3
replace leave_date_3= h_a3_leave_ph_month_3 if leave_date_3==.c
replace leave_date_3= h_a18_leave_ph_date_3 if leave_date_3==.c
replace leave_date_3= h_a18_leave_ph_month_3 if leave_date_3==.c
format %td leave_date_3
lab var leave_date_3 "Date of departure"

* Return date to the Philippines
gen return_3=.c
replace return_3=h_a5_return_ph_date_3 
replace return_3=h_a5_return_ph_month_3 if return_3==.c
format %td return_3
lab var return_3 "Date of return to the Philippines"

* End date of the FIRST contract
gen end_firstcontract_3=.c
replace end_firstcontract_3= h_a4_end_contract_date_3
replace end_firstcontract_3= h_a23_end_first_contract_date_3 if end_firstcontract_3==.c
format %td end_firstcontract_3
lab var end_firstcontract_3 "End date of first employment contract"

* End date of CURRENT contract
gen end_currentcontract_3=.c
replace end_currentcontract_3=h_a24_end_last_contract_date_3
replace end_currentcontract_3=h_a24_end_last_contract_month_3 if end_currentcontract_3==.c
format %td end_currentcontract_3
lab var end_currentcontract_3 "End date of current employment contract"

* First change of employers date 
gen date_changeemp_3=.c
replace date_changeemp_3= h_a20_date_change_emp_date_3
replace date_changeemp_3= h_a20_date_change_emp_month_3 if date_changeemp_3==.c
replace date_changeemp_3= h_dateofinterview_3-30*h_a20_date_change_emp_mos_ago_3 if date_changeemp_3==.c
format %td date_changeemp_3
lab var date_changeemp_3 "Date when HSW first changed employers"

*Financial coordination
* Note: in wave 2, we asked if they discuss amount of remittances with the family. 
* In HSW wave 3, we asked who decides on the amount and use of remittances
gen dis_budget_3=1 if h_e1_discuss_budget_3<=2
replace dis_budget_3=0 if h_e1_discuss_budget_3==3
gen disagree_use_3=h_e4_disagree_remit_3

lab var dis_budget_3 "Discussed budget"
lab var disagree_use_3 "Had disagreement on use of remittances"
lab val dis_budget_3 disagree_use_3 l_yesno

* Demand for extra remittances
gen sent_more_3=h_e17_excess_remit_3
lab var sent_more_3 "HSW sent more remittances than planned"
lab val sent_more_3 l_yesno

gen fam_extra_mon_3=h_e18_family_ask_more_3
lab var fam_extra_mon_3 "Family asked for extra money"
lab val fam_extra_mon_3 l_yesno

* Remittances
* HSW savings set aside since arrival in destination in PHP
* Using USD-variable generated in cleaning do-file
gen hsw_savings_3=0 if h_e22_set_aside_savings_3==4
replace hsw_savings_3 = h_e24_savings_salary_con_3 if h_e22_set_aside_savings_3!=4 

* HSW savings who returned to the PHP
* Using USD-variable generated in cleaning do-file
replace hsw_savings_3 =h_e23_savings_return_con_3 if h_e23_savings_return_con_3<.

* Savings set aside by family
* Using USD-variable generated in cleaning do-file
gen fam_savings_3=0 if h_e22_set_aside_savings_3==1 | h_e22_set_aside_savings_3==4
replace fam_savings_3= h_e27_savings_remit_family_con_3 if  h_e22_set_aside_savings_3!=1 & h_e22_set_aside_savings_3!=4

* Any savings
gen any_savings_3 = 0 if hsw_savings_3==0 | fam_savings_3==0
replace any_savings_3 = 1 if hsw_savings_3>0 & hsw_savings_3<.
replace any_savings_3 = 1 if fam_savings_3>0 & fam_savings_3<.
lab var any_savings_3 "Any savings"

* Total savings
gen total_savings_3=hsw_savings_3+fam_savings_3
lab var total_savings_3 "Total amount of savings (in USD)"

gen share_hsw_sav_3=hsw_savings_3/total_savings_3
gen share_fam_sav_3=fam_savings_3/total_savings_3
lab var share_hsw_sav_3 "Share of HSW savings"
lab var share_fam_sav_3 "Share of Family savings"

* Frequency of remittances in last six months
gen freq_remit_3 = h_e13_no_send_money_times_no_3
lab var freq_remit_3 "Frequency of remittances in past six months"

* Amount of remittances sent last time
* Using USD-variable generated in cleaning do-file; Results from cleaning file differ from below, and are more reliable
gen amount_remit_3=.c
replace amount_remit_3=0 if  h_e12_freq_remit_3==5 //HSW did not send money
replace amount_remit_3=h_e14_amount_send_money_con_3 if h_e12_freq_remit_3!=5

gen remit_total_3 = amount_remit_3 * freq_remit_3

label var hsw_savings_3 "HSW savings (USD)"
label var fam_savings_3 "Family savings (USD)"
label var amount_remit_3 "Remittances most recent transaction (USD)"
label var remit_total_3 "Remittances in past six months (USD)"

* Log of savings and remittances
gen l_total_sav_3=log(total_savings_3+1)
gen l_hsw_savings_3=log(hsw_savings_3+1)
gen l_fam_savings_3=log(fam_savings_3+1)
gen l_am_remit_3=log(amount_remit_3+1)
gen l_remit_total_3=log(remit_total_3+1)

label var l_total_sav_3 "Total savings (log)"
label var l_hsw_savings_3 "HSW savings (log)"
label var l_fam_savings_3 "Family savings (log)"
label var l_am_remit_3 "Remittances most recent transaction (log)"
label var l_remit_total_3 "Remittances in past six months (log)"

* Prepare communication intensity
gen text_freq_3=7 if h_b1_text_family_3==1
replace text_freq_3=5 if h_b1_text_family_3==2
replace text_freq_3=2.5 if h_b1_text_family_3==3
replace text_freq_3=1 if h_b1_text_family_3==4
replace text_freq_3=0 if h_b1_text_family_3==5

* Length of last phone call in minutes
* Note: some values are not stored in hh:mm format
gen call_freq_3 =h_b2_talk_family_no_3 
gen talk_length=clock(h_b3_length_conversation_time_3 ,"hm")
gen call_length_3=talk_length/(60*1000)

* Some other values appear to large (Cutpoint: 3 hours)
replace call_length_3=.c if call_length_3>180
drop talk_length

lab var text_freq_3 "Text frequency (in a week)"
lab var call_freq_3 "Call frequency (in a week)"
lab var call_length_3 "Length of phone conversation (in minutes)"

* Confiscate mobile phone
gen conf_phone_3=.c
replace conf_phone_3=1 if h_b6_confiscate_cel_3==1 | h_b6_confiscate_cel_3==4 // employer or recruitment agency
replace conf_phone_3=0 if h_b6_confiscate_cel_3==2
lab var conf_phone_3 "Employer attempted to confiscate phone"
lab val conf_phone_3 l_yesno

* Description of employer
rename h_d1_description_employer_good_ employer_good_3
rename h_d1_description_employer_short employer_short_3 
rename h_d1_description_employer_slave employer_slaver_3
rename h_d1_description_employer_viole employer_violent_3
rename h_d1_description_employer_stric employer_strict_3

gen noemployer_short_3 = 1 - employer_short_3
gen noemployer_slaver_3 = 1 - employer_slaver_3
gen noemployer_violent_3 = 1 - employer_violent_3
gen noemployer_strict_3 = 1 - employer_strict_3

lab val noemployer_*_3 employer_*_3 l_yesno

egen employer_subj_index_3 = rowmean(employer_good_3 noemployer_short_3 noemployer_slaver_3 noemployer_violent_3 noemployer_strict_3)

* Treatment by the employer
gen noshouting_3 = h_d4_shout_3
gen noviolence_3 = h_d6_violence_3
gen nothreat_3 = h_d7_threat_3
gen nosexharass_3 = h_d8_sexharass_3 
gen no_near_sexharass_3 = h_d9_exp_close_3
gen no_worksick_3 = h_d5_work_sick_3

gen enoughfood_3 = h_d10_provide_food_3

recode noshouting_3 noviolence_3 nothreat_3 nosexharass_3 no_near_sexharass_3 no_worksick_3 (0=1) (1=0) (98 99=.)
lab val noshouting_3 noviolence_3 nothreat_3 nosexharass_3 no_near_sexharass_3 no_worksick_3 l_yesno

lab var noshouting_3 "Experienced no shouting"
lab var noviolence_3 "Experienced no violence"
lab var nothreat_3 "Experienced no threats"
lab var nosexharass_3 "Experienced no sexual harassment"
lab var no_near_sexharass_3 "No surrounding persons experienced physical violence or sexual harassment"
lab var enoughfood_3 "Have enough food"
lab var no_worksick_3 "Not forced to work when sick"

* Compute working hours
gen work_start_3=clock(h_d12_worktime_start_time_3,"hm")
gen work_end_3=clock(h_d13_worktime_end_time_3,"hm")
format %tCHH:MM work_start_3 work_end_3

* If work_end is 00:00, I changed it to 23:59
replace work_end_3=clock("23:59", "hm") if work_end_3==clock("00:00","hm")
*if work_end is between past 12mn to 4am, add 24 hrs
replace work_end_3=work_end_3+24*60*60*1000 if hh(work_end_3)<=4

recode work_end_3 work_start_3 (nonmissing=.) if work_start_3 > work_end_3

* Generate working hours (not including break time)
gen workhours_3=(work_end_3-work_start_3)/(60*60*1000)
* Drop work_end
lab var workhours_3 "Gross working hours"

* Generate working hours (include break time)
gen breaktime_3=.c
replace breaktime_3=0 if h_d14_work_break_3==0
replace breaktime_3=clock(h_d15_hours_break_time_3,"hm") if h_d14_work_break_3==1
format %tCHH:MM breaktime_3
gen worktime_3 = workhours_3-hours(breaktime_3)
list h_d12_worktime_start_time_3 h_d13_worktime_end_time_3 h_d15_hours_break_time_3 workhours_3 worktime_3 if worktime_3!=.c
replace worktime_3=.c if worktime_3<=0 | worktime_3>=24
lab var worktime_3 "Net working hours"
gen sparetime_3 = 24-worktime_3
lab var sparetime_3 "Non-working hours"

* Rest day once a week
tab h_d16_restday_3
gen restday_3=.c
replace restday_3=1 if h_d16_restday_3==1
replace restday_3=0 if h_d16_restday_3!=1 // Includes other freq of rest days 
replace restday_3=.c if h_d16_restday_3>=.
lab var restday_3 "HSW has rest day once a week"
lab val  restday_3 l_yesno

* Leave house during rest day
gen leavehouse_3=h_d17_leave_restday_3
replace leavehouse_3=0 if restday_3==0 //also 0 if HSW has no weekly rest day
replace leavehouse_3=.c if h_d16_restday_3>=.
lab var leavehouse_3 "HSW can leave the employer's house"
lab val leavehouse_3 l_yesno

* Receive salary on time
gen salary_ontime_3=.c
replace salary_ontime_3=1 if h_e5_regular_salary_3==1
replace salary_ontime_3=0 if h_e5_regular_salary_3!=1 //includes 1-7 days delay
replace salary_ontime_3=.c if h_e5_regular_salary_3>=.
lab var salary_ontime_3 "HSW receives her salary on time"
lab val salary_ontime_3 l_yesno

* Experience salary deduction
gen sal_noded_3=.c
replace sal_noded_3=1 if h_e7_deduction_3==3
replace sal_noded_3=0 if h_e7_deduction_3!=3
replace sal_noded_3=.c if h_e7_deduction_3>=.

lab var sal_noded_3 "HSW did not experience salary deduction"
lab val sal_noded_3 l_yesno

* Relation to employer
gen employer_rel_3 = h_d2_satisfy_emp_before_3
lab var employer_rel_3 "Quality of relationship with employer (start)"

gen employer_rel_now_3 = h_d3_satisfy_emp_current_3
lab var employer_rel_now_3 "Quality of relationship with employer (now)"

gen employer_rel_av_3 = (employer_rel_3+employer_rel_now_3)/2
lab var employer_rel_av_3 "Quality of relationship with employer (average)"

* Adequate medical treatment
gen med_treat_3 = 1 if h_d19_med_treat_3==1
replace med_treat_3 = 0 if h_d19_med_treat_3==2
lab var med_treat_3 "Received adequate medical treatment"

* Well-being
gen happy_3=h_c1_how_happy_3 
recode happy_3 (1=5) (2=4) (3=3) (4=2) (5=1) (98 99=.c)
lab var happy_3 "Happy"

lab def l_reversecode 5 "All of the time" 4 "Most of the time" 3 "Some of the time" 2 "A little of the time" 1 "None of the time"
lab val happy_3 l_reversecode

gen nervous_3=h_c2_how_nervous_3 
recode nervous_3 (98 99=.c)
lab var nervous_3 "Nervous"

gen downheart_3=h_c3_how_downheart_3
recode downheart_3 (98 99=.c)
lab var downheart_3 "Down hearted and blue"

gen pain_3=h_c4_how_pain_3
recode pain_3 (98 99=.c)
lab var pain_3 "Experienced pain"

lab def l_mhcode  1 "All of the time" 2 "Most of the time" 3 "Some of the time" 4 "A little of the time" 5 "None of the time"
lab val nervous_3 downheart_3 pain_3 l_mhcode

egen mental_health_3=rowmean(happy_3 nervous_3 downheart_3 pain_3)
lab var mental_health_3 "Mental Health Index"

* Migrant specific well-being
gen homesick_3=h_c5_how_homesick_3 
recode homesick_3 (98 99=.c)
lab var homesick "Homesick"
lab val homesick l_mhcode

gen overwhelm_3=h_c6_how_overwhelm_3
recode overwhelm_3 (98 99=.c)
lab var overwhelm_3 "Overwhelm"
lab val overwhelm_3 l_mhcode

egen migrant_well_3 = rowmean(homesick_3 overwhelm_3)
lab var migrant_well_3 "Migrant specific well-being"

* Account and mode of remitting
gen account_fo_3 = h_e21_acct_abroad_3
gen account_ph_3 = h_e20_acct_ph_3
egen account_3 = rowmax(account_fo_3 account_ph_3)
gen remit_bank_3 = 0 if h_e15_remittance_channel_3<.
replace remit_bank_3 = 1 if h_e15_remittance_channel_3==1

lab var account_fo_3 "Has personal bank account abroad"
lab var account_ph_3 "Has personal bank account in Phil"
lab var remit_bank_3 "Remittances sent via bank transfer"
lab val account_fo_3 account_ph_3 remit_bank_3 l_yesno

* Marriage status
gen married_3 = 0 if h_f1_maritalstatus_3<.
replace married_3 = 1 if h_f1_maritalstatus_3==2
gen divorced_3 = 0 if h_f1_maritalstatus_3<.
replace divorced_3 = 1 if h_f1_maritalstatus_3==5
 

*Future plans
gen stay_ph_3=.c
replace stay_ph_3=1 if h_a25_plan_end_contract_3==1 // plan if HSW is abroad
replace stay_ph_3=0 if h_a25_plan_end_contract_3!=1    
replace stay_ph_3=1 if h_a17_reapply_abroad_3==0  //plan if HSW is in PH 
replace stay_ph_3=0 if h_a17_reapply_abroad_3==1
lab var stay_ph_3 "HSW plans to stay in the Philippines"
lab val stay_ph_3 l_yesno

gen stay_employer_3=.c
replace stay_employer_3=1 if h_a25_plan_end_contract_3==2 | h_a25_plan_end_contract_3==5
replace stay_employer_3=0 if h_a25_plan_end_contract_3==1 | h_a25_plan_end_contract_3==3 | h_a25_plan_end_contract_3==4 | h_a25_plan_end_contract_3==6 | h_a25_plan_end_contract_3==7
replace stay_employer_3=0 if h_a17_reapply_abroad_3==0 | h_a17_reapply_abroad_3==1
lab var stay_employer_3 "HSW plans to continue working for current employer"
lab val stay_employer_3 l_yesno

* HSW thinks it is good for her children to become an OFW
gen child_ofw_3=h_f4_open_child_ofw_3
recode child_ofw_3 (1=5) (2=4) (3=3) (4=2) (5=1) (98 99=.c)
lab def l_child_ofw_3  5 "Strongly Agree" 4 "Agree" 3 "Neither Agree nor Disagree" 2 "Disagree" 1 "Strongly Disagree"
lab val child_ofw_3 l_child_ofw_3
lab var child_ofw_3 "HSW thinks good for children to become OFW"

* Generate status of a person
* Note: we followed up in the HH wave 3 survey the status of the HSW (if in PH, finished/terminated contract. if in HK/KSA, current contract/new contract)
gen     h_status_3 = 1 if (h_a1_current_loc_3 == 2 | h_a1_current_loc_3 == 3) & h_change_emp_3==0
replace h_status_3 = 2 if (h_a1_current_loc_3 == 2 | h_a1_current_loc_3 == 3) & h_change_emp_3==1
replace h_status_3 = 3 if h_a1_current_loc_3 == 1
replace h_status_3 = 4 if h_a1_current_loc_3 == 4
replace h_status_3 = 5 if h_a2_terminated_3  == 0

lab var h_status_3 "Status at the time of wave 3 interview"
tab h_status_3, gen(h_status_3_b)

*replace . with .c
qui ds, has(type numeric)
local x=r(varlist)
recode `x' (.=.c) 

save "$data_prep/owwa_hsw_w3_edited", replace
